﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using DevComponents.DotNetBar;
using SpeechLib;
using System.Xml;
namespace QLDBNH_CLIENT
{
    class Sent_Receive
    {
 
        public void Sent(string ip, string port, string snt)
        {
            try
            {
                UdpClient send = new UdpClient();
                IPEndPoint iepRemote = new IPEndPoint(IPAddress.Parse(ip), int.Parse(port));
                byte[] data = new byte[1024];
                data = Encoding.UTF8.GetBytes(snt);
                send.Send(data, data.Length, iepRemote);
                send.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        public void Sent1(string ip, string port)
        {
            //Get host name.
            string myHost = System.Net.Dns.GetHostName();

            //Get the IP from the host name.
            string myIP = System.Net.Dns.GetHostByName(myHost).AddressList[0].ToString();

            //khai bao bien va gan gia tri.
            int flag = 0;

            DataSet sent = new DataSet();
            DataTable connect = new DataTable("connect");

            //table 'connect';
            connect.Columns.Add(new DataColumn("Khoa", typeof(System.String)));
            connect.Columns.Add(new DataColumn("ip", typeof(System.String)));
            sent.Tables.Add(connect);

            try
            {
                //ghi vao table 'connect'.
                DataRow t = sent.Tables["connect"].NewRow();
                t[0] = "12";
                t[1] = myIP;
                sent.Tables["connect"].Rows.Add(t);

                sent.Tables["connect"].WriteXml("connect.txt");
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            try
            {
                //Chuyen file connect.txt sang chuoi.
                string cnect = "";
                StreamReader r = new StreamReader("connect.txt");
                while (r.Peek() != -1)
                {
                    cnect += r.ReadLine();
                }
                r.Close();

                UdpClient send = new UdpClient();
                IPEndPoint iepRemote = new IPEndPoint(IPAddress.Parse(ip), int.Parse(port));
                byte[] data = new byte[1024];
                data = Encoding.UTF8.GetBytes(cnect);
                send.Send(data, data.Length, iepRemote);
                send.Close();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}
